
# V appendix - Figure A2 : by cancer type ------------------------------------------

monthly_cost_ageQ_func_cnr <- function(data,
                                       died,
                                       group_txt,
                                       min = min_obs_num,
                                       top_9_topo = top_9_topo) {
  
    # Bring relevant variables: 
  
  output_dt <-     data[,.(CNRS_topo_main_groups,
            DMG_died_within_365d,
            DMG_age_quintiles =   gtools::quantcut(DMG_age, seq(0, 1, 0.2)),
            prob_for_report,
            UTL_f365d_total_cost,
            num_days_lived = ifelse(DMG_died_within_365d == "0",
                                    366,
                                    as.numeric(difftime(DMG_date_of_death_XX,
                                                        S_index_date_XX, 
                                                        units = "days"))+1))][
                                                          # choose relevant group :                                                           
                                                          DMG_died_within_365d%in%died,
                                                          # calc adj. cost  by group :         
                                                          .(group = group_txt,
                                                            ave_monthly_cost = (sum(UTL_f365d_total_cost)/sum(num_days_lived))*31,
                                                            obs_num = .N),
                                                          by = .(deciles = plyr::round_any(prob_for_report, 0.2, floor),
                                                                 DMG_age_quintiles,
                                                                 Cancer_type = ifelse(CNRS_topo_main_groups%in% top_9_topo,
                                                                                      CNRS_topo_main_groups,
                                                                                      "Other"))
                                                          ][obs_num >= min]
  return(output_dt)
}

do_MonthlySpending_byPhat_andDeath_ageQ_CancerType <- function(data = test_dt_withProb_cancer , 
                         min_obs_num = min_obs_num) {
  
top_9_topo <-   data[    CNRS_topo_main_groups!="Others",
                           .N,
                           by = .(CNRS_topo_main_groups)
                           ][order(-N)][1:9]$CNRS_topo_main_groups

dt_monthly_cost_ageQ_type <- rbind(
  monthly_cost_ageQ_func_cnr(data, "1","Decedent", top_9_topo=top_9_topo),
  monthly_cost_ageQ_func_cnr(data,"0","Survivor", top_9_topo=top_9_topo),
  monthly_cost_ageQ_func_cnr(data,c("0","1"),"All", top_9_topo=top_9_topo)
  
)


if (sum(dt_monthly_cost_ageQ_type$obs_num)/
    nrow(data)<0.99) {
  stop("Problem with building dt for figure A2!")
}


dt_monthly_cost_ageQ_type[,Cancer_type   
                          := factor(Cancer_type    ,
                                    levels = c(top_9_topo,"Other"),
                                    labels = gsub(" ","\n",
                                                  gsub("+[[:space:]]+\\(secondary\\?\\)", 
                                                       "",
                                                       gsub("Hemato. and reticul. systems", 
                                                            "Hematopoietic system", 
                                                            c(top_9_topo,"Other"))))) 
                          ]


write.csv(dt_monthly_cost_ageQ_type %>% 
            mutate(Cancer_type=gsub("\n"," ",Cancer_type)),
          file = "MonthlySpending_byPhat_andDeath_ageQ_CancerType_version_B5_agelines.csv") 


pdf(file = "MonthlySpending_byPhat_andDeath_ageQ_CancerType_version_B5_agelines_all.pdf",
    height = 12) 

print(
  ggplot(dt_monthly_cost_ageQ_type[group=="All"]%>% 
           mutate(Cancer_type=gsub("\n"," ",Cancer_type)) ,
         aes(x = deciles ,
             y = ave_monthly_cost,
             color =  DMG_age_quintiles  )) +
    geom_line(alpha=0.75) + 
    facet_wrap( ~Cancer_type ,
                scales = "free",ncol = 2, nrow = 5) +
    scale_color_grey(start = 0.9,end = 0.2 ,
                     labels = c(levels(dt_monthly_cost_ageQ_type$DMG_age_quintiles)),
                     name="Age Quintile",
                     guide=guide_legend(ncol=1)) +
    theme(strip.text.x = element_text(size =12),
          strip.text.y = element_text(size =12),
          axis.text.x = element_text(size =10),
          axis.text.y = element_text(size =10)) +
    scale_x_continuous( limits = c(0,0.8) , breaks = seq(0,0.8,0.2)) +
    scale_y_continuous(labels = scales::comma, expand = c(0,0)) +
    expand_limits(x = 0, y = 0)+
    labs(x = "Predicted Mortality Risk", 
         y = "Average Monthly Spending (NIS)",
         color = "Age Quintile")
)
dev.off()



pdf(file = "MonthlySpending_byPhat_andDeath_ageQ_CancerType_version_B5_agelines_S.pdf",
    height = 12) 

print(
  ggplot(dt_monthly_cost_ageQ_type[group=="Survivor"]%>% 
           mutate(Cancer_type=gsub("\n"," ",Cancer_type)) ,
         aes(x = deciles ,
             y = ave_monthly_cost,
             color =  DMG_age_quintiles  )) +
    geom_line(alpha=0.75) + 
    facet_wrap( ~Cancer_type ,
                scales = "free",ncol = 2, nrow = 5) +
    scale_color_grey(start = 0.9,end = 0.2 ,
                     labels = c(levels(dt_monthly_cost_ageQ_type$DMG_age_quintiles)),
                     name="Age Quintile",
                     guide=guide_legend(ncol=1)) +
    theme(strip.text.x = element_text(size =12),
          strip.text.y = element_text(size =12),
          axis.text.x = element_text(size =10),
          axis.text.y = element_text(size =10)) +
    scale_x_continuous( limits = c(0,0.8) , breaks = seq(0,0.8,0.2)) +
    scale_y_continuous(labels = scales::comma, expand = c(0,0)) +
    expand_limits(x = 0, y = 0)+
    labs(x = "Predicted Mortality Risk", 
         y = "Average Monthly Spending (NIS)",
         color = "Age Quintile")
)
dev.off()


pdf(file = "MonthlySpending_byPhat_andDeath_ageQ_CancerType_version_B5_agelines_D.pdf",
    height = 12) 

print(
  ggplot(dt_monthly_cost_ageQ_type[group=="Decedent"]%>% 
           mutate(Cancer_type=gsub("\n"," ",Cancer_type)) ,
         aes(x = deciles ,
             y = ave_monthly_cost,
             color =  DMG_age_quintiles  )) +
    geom_line(alpha=0.75) + 
    facet_wrap( ~Cancer_type ,
                scales = "free",ncol = 2, nrow = 5) +
    scale_color_grey(start = 0.9,end = 0.2 ,
                     labels = c(levels(dt_monthly_cost_ageQ_type$DMG_age_quintiles)),
                     name="Age Quintile",
                     guide=guide_legend(ncol=1)) +
    theme(strip.text.x = element_text(size =12),
          strip.text.y = element_text(size =12),
          axis.text.x = element_text(size =10),
          axis.text.y = element_text(size =10)) +
    scale_x_continuous( limits = c(0,0.8) , breaks = seq(0,0.8,0.2)) +
    scale_y_continuous(labels = scales::comma, expand = c(0,0)) +
    expand_limits(x = 0, y = 0)+
    labs(x = "Predicted Mortality Risk", 
         y = "Average Monthly Spending (NIS)",
         color = "Age Quintile")
)
dev.off()

}